#include <QPluginLoader>
#include <QMessageBox>
#include <QApplication>
#include <QFileDialog>
#include <QMenu>
#include <QMenuBar>
#include <QBoxLayout>
#include <QDebug>
#include <QtConcurrentRun>

#include "mainwindow.h"
#include "pluginswidget.h"
#include "logfileadapter.h"
#include "arsadapter.h"
//#include "projectbayadapter.h"

////////// MainWindow //////////////////////////////////////////////////////////////////////////////

MainWindow::MainWindow()
    : QMainWindow()
{
//    m_useCaseModel = 0;

//    m_tmp_model = 0;
//    m_tmp_view = 0;
    setWindowTitle(tr("ARS Illustrator"));
    loadStyleSheets();

    createMainWigets();
    loadPlugins();
    createDialogs();
    createMenus();
    initViewToolBar();
    loadSettings();
    setStyleSheet(m_styleSheets.value("main"));

    showMaximized();
    addTab(ViewWidget::DummyView, tr("Home Page"));
}

QAction * MainWindow::addViewToolButton(const QString & name, QIcon icon)
{
    QAction * action = new QAction(icon, name, this);
    m_viewToolBar->addAction(action);

    return action;
}

TabWidget * MainWindow::currentTabWidget() const
{
    return qobject_cast<TabWidget *>(m_tabsStack->currentWidget());
}

QString MainWindow::currentView() const
{
    return currentTabWidget()->currrentView();
}

TabBar * MainWindow::tabBar() const
{
    return m_tabBar;
}

ViewToolbar * MainWindow::viewBar() const
{
    return m_viewToolBar;
}

QString MainWindow::getOpenFileName()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open log file"), m_lastOpenedDir,
                                                    "*.log");
    return fileName;
}

void MainWindow::openFile()
{
    openFile(QString());
}

void MainWindow::openFile(const QString & fileName)
{
    QString path;

    if(fileName.isNull())
        path = getOpenFileName();
    else
        path = fileName;

    if(path.isNull())
        return;

    QFile * logFile = new QFile(path);
    if(!logFile->open(QIODevice::ReadOnly))
    {
        QMessageBox::critical(this, tr("ARS Illustrator"), tr("Cannot open log file") + " " + path);
        delete logFile;
        return;
    }

    QFutureWatcher<DynamicModel *> * watcher = new QFutureWatcher<DynamicModel *>();
    connect(watcher, SIGNAL(finished()), this, SLOT(openFileFinished()));
    QFuture<DynamicModel *> future = QtConcurrent::run(m_iLogAdapterPlugin, &DynamicModelAdapterPluginInterface::createModel,
                                       logFile);
    watcher->setFuture(future);
    m_inProcessPool.insert(watcher, logFile);
}

void MainWindow::saveFile(const QString & fileName)
{

}

#ifdef SERVER_VERSION

void MainWindow::openServerTab()
{
    ServerModel * model = m_iServerAdapterPlugin->createModel();
    if(!model)
    {
        QMessageBox::critical(this, tr("ARS Illustrator"),
                              tr("Cannot retrieve information from server"));
        return;
    }

    int index = addTab(tr("Server"));
    TabWidget * tab = m_tabsStack->tab(index);

    foreach(ViewPluginInterface * iViewPlugin, m_viewPlugins)
    {
        if(iViewPlugin->viewType() == ViewWidget::ServerView)
        {
            ServerViewWidget * viewWidget = qobject_cast<ServerViewWidget *>(iViewPlugin->createView(this));
            if(viewWidget)
            {
                viewWidget->setModel(model);
                tab->addView(viewWidget);
            }
        }
    }

    setTab(index);
    m_viewToolBar->setView("workflowtreeview");
}

#endif // SERVER_VERSION

void MainWindow::showPluginsWidget()
{
    PluginsWidget * pluginsWidget = new PluginsWidget(this);
    foreach(ViewPluginInterface * iPlugin, m_viewPlugins)
        pluginsWidget->addPlugin(iPlugin->name());

    pluginsWidget->exec();
}

void MainWindow::showSettingsDialog()
{
    m_settingsDialog->exec();
}

int MainWindow::addTab(ViewWidget::ViewType type, const QString & tabName)
{
    TabWidget * tab = new TabWidget(this);
    int index = -1;

    foreach(ViewPluginInterface * iViewPlugin, m_viewPlugins)
    {
        if(iViewPlugin->viewType() == type)
        {
            ViewWidget * viewWidget = iViewPlugin->createView(this);
            connect(viewWidget, SIGNAL(openFile(QString)), this, SLOT(openFile(QString)));
            tab->addView(viewWidget);
        }
    }

    m_tabsStack->addWidget(tab);
    index = m_tabBar->addTab(tabName);

    return index;
}

void MainWindow::changeView(const QString & viewName)
{
    currentTabWidget()->setView(viewName);
    adjustToolBar();
}

void MainWindow::setTab(int index)
{
    TabWidget * currentTabWidget = m_tabsStack->currentTab();

    if(currentTabWidget)
    {
        QList<ViewWidget *> viewWidgets = currentTabWidget->viewWidgets();
        foreach(ViewWidget * viewWidget, viewWidgets)
            viewWidget->disconnectToolBarItems(m_toolBar->items(viewWidget->pluginName()));
    }

    m_tabsStack->setCurrentIndex(index);
    currentTabWidget = m_tabsStack->currentTab();

    if(currentTabWidget)
    {
        QList<ViewWidget *> viewWidgets = currentTabWidget->viewWidgets();
        foreach(ViewWidget * viewWidget, viewWidgets)
            viewWidget->connectToolBarItems(m_toolBar->items(viewWidget->pluginName()));
    }

    if(sender() != m_tabBar)
        m_tabBar->setCurrentIndex(index);
    m_viewToolBar->setView(m_tabsStack->currentTab()->currrentView());

    adjustToolBar();
    adjustViewBar();
}

void MainWindow::closeTab(int index)
{
    if(index == 0)
        return;

    setTab(index - 0);
    m_tabBar->removeTab(index);
    m_tabsStack->removeTab(index);
}

void MainWindow::adjustViewBar()
{
    TabWidget * currentTabWidget = m_tabsStack->currentTab();
    QStringList viewNamesList = currentTabWidget->viewNames();

    foreach (ViewPluginInterface * iView, m_viewPlugins)
        m_viewToolBar->setViewVisible(iView->name(), viewNamesList.contains(iView->name()));

    m_viewToolBar->setView(currentTabWidget->currrentView());

}

void MainWindow::adjustToolBar()
{
    TabWidget * currentTabWidget = m_tabsStack->currentTab();
    ViewWidget * viewWidget = currentTabWidget->currentViewWidget();

    m_toolBar->hideAllViewItems();

    foreach(ToolBarItem * item, m_toolBar->items(viewWidget->pluginName()))
        item->action()->setVisible(true);

    viewWidget->adjustToolbar(m_toolBar->items(viewWidget->pluginName()));
}

void MainWindow::openDynamicItem(ARSExecItem * item)
{
    m_viewToolBar->setView("treeview");
    TabWidget * tabWidget = m_tabsStack->currentTab();
    DynamicViewWidget * viewWidget = qobject_cast<DynamicViewWidget *>(tabWidget->currentViewWidget());
    if(viewWidget)
        viewWidget->gotoItem(item);
}

//void MainWindow::loadUseCase(UseCase * uc)
//{
//    m_iProjectBayAdapterPlugin->loadUseCase(uc);
//}

//void MainWindow::saveUseCase(UseCase * uc)
//{
//    m_iProjectBayAdapterPlugin->saveUseCase(uc);
//}

void MainWindow::openFileFinished()
{
    QFutureWatcher<DynamicModel *> * futureWatcher = static_cast<QFutureWatcher<DynamicModel *> *>(sender());
    if(!futureWatcher)
        return;

    QFile * logFile = m_inProcessPool.value(futureWatcher);
    m_inProcessPool.remove(futureWatcher);
    logFile->close();

    QFileInfo logFileInfo = QFileInfo(*logFile);
    DynamicModel * model = futureWatcher->result();

    if(model == 0)
    {
        QMessageBox::critical(this, tr("ARS Illustrator"), tr("Cannot open log file") + " " + logFileInfo.path());

        delete futureWatcher;
        delete logFile;
        return;
    }

    int tabIndex = addTab(ViewWidget::DynamicModelView, logFileInfo.fileName());
    attachModel(tabIndex, model);
    setTab(tabIndex);
    m_viewToolBar->setView("treeview");
    AppEventOpenFile openFileEvent(logFileInfo.path(), QDateTime::currentDateTime());
    sendEvent(&openFileEvent);
    m_lastOpenedDir = logFileInfo.dir().path();
    saveSettings();

    delete futureWatcher;
    delete logFile;
}

QString MainWindow::libFileName(const QString & libName)
{
#ifdef Q_OS_LINUX
    return libName + ".so";
#endif

#ifdef Q_OS_WIN32
    return libName + ".dll";
#endif

    return libName;
}

void MainWindow::loadStyleSheets()
{
    QStringList styleSheets;
    styleSheets \
    << "viewbar"
    << "toolbar"
    << "sequencediagramview"
    << "searchlineedit"
    << "packageview"
    << "objectslist"
    << "mainview"
    << "main"
    << "loading"
    << "itemdetailsmenu"
    << "homepage"
    << "flatstyle"
    << "filestabbar"
    << "documentblockedit"
    << "dialog"
    << "commentedit"
    << "comment"
    << "arsobjecttooltip";


    foreach(QString styleSheetName, styleSheets)
    {
        QFile styleFile(":/qss/" + styleSheetName);
        styleFile.open(QIODevice::ReadOnly);
        m_styleSheets.insert(styleSheetName, styleFile.readAll());
        styleFile.close();
    }
}

void MainWindow::createMainWigets()
{
    QWidget * centralWidget = new QWidget(this);
    QVBoxLayout * centralLayout = new QVBoxLayout(centralWidget);
    centralLayout->setContentsMargins(0, 0, 0, 0);
    centralLayout->setSpacing(0);

    m_tabBar = new TabBar(this);
    m_tabBar->setStyleSheet(m_styleSheets.value("filestabbar"));
    centralLayout->addWidget(m_tabBar);

    m_toolBar = new ToolBar(this);
    m_toolBar->setOrientation(Qt::Horizontal);
    m_toolBar->setStyleSheet(m_styleSheets.value("toolbar"));
    centralLayout->addWidget(m_toolBar);

    QHBoxLayout * hLayout = new QHBoxLayout(this);
    hLayout->setContentsMargins(0, 0, 0, 0);

    m_viewToolBar = new ViewToolbar(centralWidget);
//    m_viewToolBar->setOrientation(Qt::Vertical);
    m_viewToolBar->setStyleSheet(m_styleSheets.value("viewbar"));
    connect(m_viewToolBar, SIGNAL(viewChanged(QString)), this, SLOT(changeView(QString)));
    hLayout->addWidget(m_viewToolBar);

    m_tabsStack = new TabsStackWidget(centralWidget);
    hLayout->addWidget(m_tabsStack, 1);

    centralLayout->addLayout(hLayout, 1);

    connect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(setTab(int)));
    connect(m_tabBar, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));

    centralWidget->setLayout(centralLayout);
    setCentralWidget(centralWidget);
}

void MainWindow::createDialogs()
{
    m_settingsDialog = new SettingsDialog(this);
    foreach(ViewPluginInterface * iView, m_viewPlugins)
    {
        SettingsWidget * settingsWidget = iView->settingsWidget(m_settingsDialog);
        if(settingsWidget)
            m_settingsDialog->addSettingsWidget(settingsWidget);
    }
}

void MainWindow::createMenus()
{
    QMenu * menu = new QMenu(tr("File"), this);
    QAction * action = new QAction(QIcon(":/icons/openfile"), tr("Open"), this);
    action->setShortcut(QKeySequence("Ctrl+O"));
    connect(action, SIGNAL(triggered()), this, SLOT(openFile()));
    menu->addAction(action);
    m_toolBar->addAction(action);

#ifdef SERVER_VERSION
    action = new QAction(tr("Open Server Worksheet"), this);
    connect(action, SIGNAL(triggered()), this, SLOT(openServerTab()));
    menu->addAction(action);
    m_toolBar->addAction(action);
#endif // SERVER_VERSION

    menuBar()->addMenu(menu);

    menu = new QMenu(tr("Options"), this);
    action = new QAction(QIcon(":/icons/settings"), tr("Options"), this);
    connect(action, SIGNAL(triggered()), this, SLOT(showSettingsDialog()));
    menu->addAction(action);
    m_toolBar->addAction(action);

    menuBar()->addMenu(menu);

    m_toolBar->addSeparator();

    foreach(ViewPluginInterface * iViewPlugin, m_viewPlugins)
    {
        ToolBarItemList items = iViewPlugin->toolBarItems(this);
        m_toolBar->addItems(items, iViewPlugin->name());
    }

    menu = new QMenu(tr("Plugins"), this);
    action = new QAction(tr("Plugins"), this);
    connect(action, SIGNAL(triggered()), this, SLOT(showPluginsWidget()));
    menu->addAction(action);
    menuBar()->addMenu(menu);
}

void MainWindow::loadPlugins()
{
    loadCorePlugins();

    QDir pluginsDir("plugins");
    if(!pluginsDir.exists())
    {
        QMessageBox::critical(this, tr("ARS Illustrator"), tr("Cannot open plugins folder"));
        QApplication::quit();
    }

    foreach(QString fileName, pluginsDir.entryList())
    {
        if(QLibrary::isLibrary(fileName))
            loadPlugin(pluginsDir.filePath(fileName));
    }
}

void MainWindow::loadCorePlugins()
{
    m_iLogAdapterPlugin = new LogFileAdapter(this);
    m_iLogAdapterPlugin->loadSettings();

#ifdef SERVER_VERSION
    m_iServerAdapterPlugin = new ARSAdapter(this);
    m_iServerAdapterPlugin->connectToDatabase("10.151.135.201", 51521, "EWMS2", "aradmin", "ar#admin#");
#endif // SERVER_VERSION

//    m_iProjectBayAdapterPlugin = new ProjectBayAdapter();
//    if(!m_iProjectBayAdapterPlugin->connectToDatabase())
//        qDebug("Cannot connect to the Project Bay :(");
//    else
//    {
//        m_useCaseModel = m_iProjectBayAdapterPlugin->createModel();
//        if(!m_useCaseModel)
//            qDebug("Cannot retrieve use case model :(");
//    }
}

QObject * MainWindow::loadCorePlugin(const QString & fileName)
{
    QFile pluginFile(fileName);
    if(!pluginFile.exists())
    {
        QMessageBox::critical(this, tr("ARS Illustrator"), tr("Cannot find plugin") + " " + fileName);
        QApplication::quit();
    }

    QPluginLoader loader(fileName);
    QObject * plugin = loader.instance();
    if(!plugin)
    {
        QMessageBox::critical(this, tr("ARS Illustrator"), tr("Cannot load plugin") + " " + fileName + ". " + loader.errorString());
        QApplication::quit();
    }

    return plugin;
}

void MainWindow::loadPlugin(const QString & fileName)
{
    QPluginLoader loader(fileName);
    QObject * plugin = loader.instance();
    if(!plugin)
    {
        QMessageBox::critical(this, tr("ARS Illustrator"), tr("Cannot load plugin") + " " + fileName);
        QApplication::quit();
    }

    ViewPluginInterface  * iPlugin = qobject_cast<ViewPluginInterface *>(plugin);
    if(!iPlugin)
    {
        QMessageBox::critical(this, tr("ARS Illustrator"), tr("Cannot load plugin") + " " + fileName);
        QApplication::quit();
    }

    if(!iPlugin->loadSettings())
    {
        QMessageBox::critical(this, tr("ARS Illustrator"), tr("Cannot load settings for plugin") + " " + fileName);
        QApplication::quit();
    }

    m_viewPlugins.append(iPlugin);
}

void MainWindow::initViewToolBar()
{
    foreach(ViewPluginInterface * iView, m_viewPlugins)
        m_viewToolBar->addView(iView->viewName(), iView->icon(true), iView->icon(false), iView->name());
}

void MainWindow::attachModel(int tabIndex, DynamicModel * model)
{
    TabWidget * tabWidget = m_tabsStack->tab(tabIndex);
    if(!tabWidget)
        return;

    foreach(ViewWidget * viewWidget, tabWidget->viewWidgets())
    {
        DynamicViewWidget * dynamicViewWidget = qobject_cast<DynamicViewWidget *>(viewWidget);
        {
            dynamicViewWidget->setModel(model);
            connect(viewWidget, SIGNAL(openItem(ARSExecItem*)), this, SLOT(openDynamicItem(ARSExecItem*)));
        }
    }
}

void MainWindow::sendEvent(AppEvent * event)
{
    foreach(ViewPluginInterface * plugin, m_viewPlugins)
        plugin->appEvent(event);
}

bool MainWindow::loadSettings()
{
    QFile settingsFile("coresettings.xml");
    if(!settingsFile.open(QFile::ReadOnly))
        return false;

    QDomDocument doc;
    if(!doc.setContent(settingsFile.readAll()))
        return false;

    settingsFile.close();

    QDomElement rootElement = doc.firstChildElement();
    QDomElement keyElement = rootElement.firstChildElement();

    while(!keyElement.isNull())
    {
        if(keyElement.attribute("name") == "lastOpenedDir")
            m_lastOpenedDir = keyElement.attribute("value");

        keyElement = keyElement.nextSiblingElement();
    }

    return true;
}

void MainWindow::saveSettings()
{
    QFile settingsFile("coresettings.xml");
    if(!settingsFile.open(QFile::WriteOnly))
        return;

    QDomDocument doc;
    QDomElement rootElement = doc.createElement("settings");
    doc.appendChild(rootElement);

    QDomElement keyElement = doc.createElement("key");
    keyElement.setAttribute("name", "lastOpenedDir");
    keyElement.setAttribute("value", m_lastOpenedDir);
    rootElement.appendChild(keyElement);

    settingsFile.write(doc.toString(4).toAscii());

    return;
}

////////// TabBar //////////////////////////////////////////////////////////////////////////////////

TabBar::TabBar(QWidget * parent)
    : QTabBar(parent)
{
    setExpanding(false);
    setTabsClosable(true);
}

////////// ViewTab /////////////////////////////////////////////////////////////////////////////////

ViewTab::ViewTab()
{
    isVisible = false;
    index = -1;
}

ViewTab::ViewTab( const QString & viewName, const QIcon & icon, const QString & pluginName, int index  )
{
    this->viewName = viewName;
    this->icon = icon;
    this->pluginName = pluginName;
    this->index = index;
    this->isVisible = true;
}

bool ViewTab::isNull() const
{
    return (index == -1);
}

////////// ViewToolbar /////////////////////////////////////////////////////////////////////////////

ViewToolbar::ViewToolbar(QWidget * parent)
    : QTabBar(parent)
{
//    m_group = new QActionGroup(this);
//    setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
//    setIconSize(QSize(20, 20));
    setShape(QTabBar::RoundedWest);
    setExpanding(false);
    setIconSize(QSize(24, 24));
    connect(this, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
}

int ViewToolbar::addView(const QString & name, const QIcon & activeIcon, const QIcon & inactiveIcon, const QString & pluginName)
{
    disconnect(this, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
    int index = addTab(activeIcon, name);
    setTabData(index, pluginName);
    m_viewTabs.append(new ViewTab(name, activeIcon, pluginName, index));
    connect(this, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
//    QAction * action = new QAction(icon, name, this);
//    action->setCheckable(true);
//    action->setData(pluginName);
//    connect(action, SIGNAL(toggled(bool)), this, SLOT(viewToggled(bool)));
//    m_group->addAction(action);
//    addAction(action);
//    m_viewActions.append(action);

    return index;
}

void ViewToolbar::setView(const QString & pluginName)
{
    if(view() == pluginName)
        return;

    setCurrentIndex(viewIndex(pluginName));
}

//QAction * ViewToolbar::viewAction(const QString & viewName) const
//{
//    foreach(QAction * action, m_viewActions)
//        if(action->data().toString() == viewName)
//            return action;

//    return 0;
//}

//QList<QAction *> ViewToolbar::viewActions(bool activeOnly) const
//{
//    if(!activeOnly)
//        return m_viewActions;

//    QList<QAction *> activeActions;
//    foreach(QAction * action, m_viewActions)
//        if(action->isVisible())
//            activeActions.append(action);

//    return activeActions;
//}

QString ViewToolbar::view() const
{
    return tabData(currentIndex()).toString();
}

void ViewToolbar::tabChanged(int index)
{
    emit viewChanged(tabData(index).toString());
}

void ViewToolbar::setViewVisible(const QString & pluginName, bool visible)
{
    if(visible)
    {
        int beforeIndex = 0;
        ViewTab * tab = 0;

        for(int i = 0; i < m_viewTabs.count(); i++)
        {
            ViewTab * currentTab = m_viewTabs.at(i);
            if(currentTab->isVisible)
                beforeIndex = currentTab->index;

            if(tab)
                currentTab->index++;
            else
                if(currentTab->pluginName == pluginName)
                {
                    if(currentTab->isVisible)
                        return;

                    currentTab->isVisible = true;
                    tab = currentTab;
                }
        }

        insertTab(tab->index, tab->icon, tab->viewName);
        setTabData(tab->index, pluginName);
    }
    else
    {
        ViewTab * tab = 0;

        for(int i = 0; i < m_viewTabs.count(); i++)
        {
            ViewTab * currentTab = m_viewTabs.at(i);

            if(tab)
                currentTab->index--;
            else
                if(currentTab->pluginName == pluginName)
                {
                    if(!currentTab->isVisible)
                        return;

                    currentTab->isVisible = false;
                    tab = currentTab;
                }
        }

        removeTab(tab->index);
    }
}

int ViewToolbar::viewIndex(const QString & pluginName)
{
    for(int i = 0; i < count(); i++)
    {
        if(tabData(i).toString() == pluginName)
            return i;
    }

    return -1;
}

////////// ToolBar /////////////////////////////////////////////////////////////////////////////////

ToolBar::ToolBar(QWidget * parent)
    : QToolBar(parent)
{

}

void ToolBar::addItems(ToolBarItemList itemList, const QString & viewName)
{
    foreach(ToolBarItem * item, itemList)
    {
        if(item->type() == ToolBarItem::Widget)
        {
            ToolBarWidget * toolBarWidget = qobject_cast<ToolBarWidget *>(item);
            item->setAction(addWidget(toolBarWidget->widget()));
        }
        else
            addAction(item->action());
        m_items.insertMulti(viewName, item);
    }
}

ToolBarItemList ToolBar::items(const QString & viewName)
{
    if(viewName.isNull())
        return m_items.values();

    return m_items.values(viewName);
}

void ToolBar::setItemsVisible(const QString &viewName, bool visible)
{
    foreach(ToolBarItem * item, m_items.values(viewName))
        item->action()->setVisible(visible);
}

void ToolBar::hideAllViewItems()
{
    foreach(ToolBarItem * item, m_items.values())
        item->action()->setVisible(false);
}

////////// TabWidget ///////////////////////////////////////////////////////////////////////////////

TabWidget::TabWidget(QWidget * parent)
    : QStackedWidget(parent)
{
}

void TabWidget::addView(ViewWidget * viewWiget)
{
    addWidget(viewWiget);
}

ViewWidget * TabWidget::currentViewWidget() const
{
    return qobject_cast<ViewWidget *>(currentWidget());
}

QString TabWidget::currrentView() const
{
    return currentViewWidget()->pluginName();
}

QStringList TabWidget::viewNames() const
{
    QStringList viewList;

    for(int i = 0; i < count(); i++)
    {
        ViewWidget * viewWidget = qobject_cast<ViewWidget *>(widget(i));
        if(viewWidget)
            viewList.append(viewWidget->pluginName());
    }

    return viewList;
}

QList<ViewWidget *> TabWidget::viewWidgets() const
{
    QList<ViewWidget *> result;
    for(int i = 0; i < count(); i++)
    {
        ViewWidget * viewWidget = qobject_cast<ViewWidget *>(widget(i));
        if(viewWidget)
            result.append(viewWidget);
    }

    return result;
}

void TabWidget::setView(const QString & pluginName)
{
    for(int i = 0; i < count(); i++)
    {
        ViewWidget * view = qobject_cast<ViewWidget *>(widget(i));
        if(view->pluginName() == pluginName)
        {
            setCurrentWidget(view);
            return;
        }
    }
}

////////// TabsStackWidget /////////////////////////////////////////////////////////////////////////

TabsStackWidget::TabsStackWidget(QWidget * parent)
    : QStackedWidget(parent)
{

}

TabWidget * TabsStackWidget::tab(int index)
{
    QWidget * w = widget(index);
    if(!w)
        return 0;

    return qobject_cast<TabWidget *>(w);
}

TabWidget * TabsStackWidget::currentTab()
{
    return tab(currentIndex());
}

void TabsStackWidget::removeTab(int index)
{
    TabWidget * w = tab(index);

    if(w)
        removeWidget(tab(index));
}

////////////////////////////////////////////////////////////////////////////////////////////////////
